package controller;


import config.Interceptor.PassToken;
import entity.TokenDao;
import entity.User;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import service.impl.Userserviceimpl;
import utils.CommonRetumType;
import utils.JwtUtils;
import utils.RedisUtil;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@CrossOrigin
public class loginController extends BaseController{
    @Resource
    Userserviceimpl userserviceimpl;

    @Resource
    RedisUtil redisUtil;

    @PassToken
    @PostMapping("/login")
    public CommonRetumType login(@RequestParam("username") String username,
                                 @RequestParam("password") String password)
    {
        HashMap<String,String> site= new HashMap<String,String>();
        User user1 = userserviceimpl.login(username, password);
        String token = JwtUtils.createToken(user1.getId().toString(),user1.getRoleId().toString(),user1.getUsername(),password);

        TokenDao tokenDao = new TokenDao();
        tokenDao.setToken(token);
        redisUtil.set("token",token);
        site.put("token",token);
        site.put("roleid",user1.getRoleId().toString());
        return CommonRetumType.create(site,"登录成功");
    }
}
